package com.example.demo.controller;

import com.example.demo.pojo.PublicFields;
import com.example.demo.pojo.Result;
import com.example.demo.pojo.dto.ComponentDTO;
import com.example.demo.pojo.query.ComponentQuery;
import com.example.demo.pojo.vo.ComponentVO;
import com.example.demo.pojo.vo.PageVO;
import com.example.demo.service.imply.IComponentsServiceImply;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/component")
@Slf4j
@Api(tags = "产品相关接口")
@RequiredArgsConstructor
@CrossOrigin(origins = {"http://localhost:8080"}, allowCredentials = "true")
public class ComponentsController {
    private final IComponentsServiceImply componentService;
    @ApiOperation("添加产品")
    @PostMapping("/add")
    public Result addDevice(@RequestBody ComponentDTO componentDTO){

        componentService.addComponent(componentDTO, PublicFields.newInstance());

        return Result.success("新增成功");
    }

    @ApiOperation("批量删除产品")
    @PostMapping("/delete")
    public Result deleteDevice(@RequestBody List<Long> ids){

        componentService.deleteComponent(ids);

        return Result.success("删除成功");
    }

    @ApiOperation("修改产品")
    @PostMapping("/update")
    public Result deleteDevice(@RequestBody ComponentDTO deviceDTO){

        componentService.updateComponent(deviceDTO, PublicFields.newInstance());

        return Result.success("修改成功");
    }

    @ApiOperation("分页查询")
    @PostMapping("/page")
    public Result<PageVO<ComponentVO>> page(ComponentQuery deviceQuery){
        System.out.println(deviceQuery);
        return Result.success(componentService.queryComponentPage(deviceQuery));
    }

    @ApiOperation("通过产品id查询产品")
    @GetMapping("/{id}")
    public Result<ComponentVO> getDeviceById(@PathVariable("id") Long id){

        ComponentVO deviceVO = componentService.getComponentById(id);
        return Result.success(deviceVO);
    }
}
